package.path  = "../dnnode/?.lua;" .. package.path

local baselogic = require "robotlogic/baselogic"
local skynet = require "skynet"
local tabletool = require "tabletool"
local timetool = require "timetool"
local base = require "base"
local filelog = require "filelog"

require "dnenum"

local cardTool = require "dncardtool"

local RobotLogic = baselogic:new()


local ready_delay_weights = {
    [1] = 40,   --1s
    [2] = 30,   --2s
    [3] = 20,   --3s
    [4] = 10,   --4s
    --[5] = 5,    --5s
}

--
--doaction延时时间权值表
--
local doaction_delay_weights = {
    [1] = 40,   --1s
    [2] = 25,   --2s
    [3] = 25,   --3s
    [4] = 10,   --4s
    ---[5] = 10,   --5s
}

local free_qiang_weights = {
    [1] = 40,
    [2] = 30,
    [3] = 20,
    [4] = 10,
}

RobotLogic._ready_delay = ready_delay_weights
RobotLogic._doaction_delay = doaction_delay_weights

--
--计算总权值
--
local function calculate_total_weight(w_list)
    local total_weight = 0
    for id,w in pairs(w_list) do
        total_weight = total_weight + w
    end

    return total_weight
end

--根据权值表，获取随机结果
local function get_random_result_by_weightlist(weight_list, total_w)
    total_w = total_w or calculate_total_weight(weight_list)
    local r = base.get_random(1,total_w)
    local sum = 0
    for id,w in pairs(weight_list) do
        sum = sum + w
        if r <= sum then
            return id
        end
    end
    return   
end

local function get_free_qiang_times(total, self_info, other_info, is_open_flush)
    -- body
    local self_temp_seat = {
        cards = tabletool.deepcopy(self_info.cards),
        card_type = 0,
        final_cards = tabletool.deepcopy(self_info.cards),
    }
    local temp_temp_cards = tabletool.deepcopy(self_info.cards)
    cardTool.analysis_card_type(self_temp_seat, temp_temp_cards, is_open_flush)
    local other_temp_seats = {}
    local is_biggest = true
    for index, otherinfo in pairs(other_info) do
        if other_temp_seats[otherinfo.rid] == nil then other_temp_seats[otherinfo.rid] = {} end
        other_temp_seats[otherinfo.rid].cards = tabletool.deepcopy(otherinfo.cards)
        other_temp_seats[otherinfo.rid].card_type = 0
        other_temp_seats[otherinfo.rid].final_cards = tabletool.deepcopy(otherinfo.cards)
        local temp_card = tabletool.deepcopy(otherinfo.cards)
        cardTool.analysis_card_type(other_temp_seats[otherinfo.rid], temp_card, is_open_flush)
        if self_temp_seat.card_type < other_temp_seats[otherinfo.rid].card_type then
            is_biggest = false
        elseif self_temp_seat.card_type == other_temp_seats[otherinfo.rid].card_type then
            local flag = cardTool.compare_card_type(self_temp_seat, other_temp_seats[otherinfo.rid])
            if flag == false then is_biggest = false end
        end 
    end
    ---当机器人是最大牌型时抢4倍
    if is_biggest == true then
        return 4
    end
    local total_w = total or calculate_total_weight(free_qiang_weights)
    local r = base.get_random(1, total_w)
    local sum = 0
    for id, w in pairs(free_qiang_weights) do
        sum = sum + w
        if r <= sum then return id end
    end
    return nil
end

local function get_xiazhu_times(self_info, other_info, banker_index, is_open_flush)
    local self_temp_seat = {
        cards = tabletool.deepcopy(self_info.cards),
        card_type = 0,
        final_cards = tabletool.deepcopy(self_info.cards),
    }
    local temp_temp_cards = tabletool.deepcopy(self_info.cards)
    cardTool.analysis_card_type(self_temp_seat, temp_temp_cards, is_open_flush)
    local other_temp_seats = {}
    local is_biggest = true
    for index, otherinfo in pairs(other_info) do
        if other_temp_seats[otherinfo.rid] == nil then other_temp_seats[otherinfo.rid] = {} end
        other_temp_seats[otherinfo.rid].cards = tabletool.deepcopy(otherinfo.cards)
        other_temp_seats[otherinfo.rid].card_type = 0
        other_temp_seats[otherinfo.rid].final_cards = tabletool.deepcopy(otherinfo.cards)
        local temp_card = tabletool.deepcopy(otherinfo.cards)
        cardTool.analysis_card_type(other_temp_seats[otherinfo.rid], temp_card, is_open_flush)
        if otherinfo.seat_index == banker_index then
            if self_temp_seat.card_type < other_temp_seats[otherinfo.rid].card_type then
                is_biggest = false
            elseif self_temp_seat.card_type == other_temp_seats[otherinfo.rid].card_type then
                local flag = cardTool.compare_card_type(self_temp_seat, other_temp_seats[otherinfo.rid])
                if flag == false then is_biggest = false end
            end
        end 
    end
    ---当机器人是最大牌型时抢4倍
    if is_biggest == true then
        return 3
    else
        return 1
    end
end

local function get_can_qiang(money, tableconf, times, in_game_num)
    local is_can_qiang = true
    if tableconf.table_type == DNTableType.TABLE_TYPE_TRAIN then return is_can_qiang end
    local qiang_times_must = {}
    for index, xia_times in ipairs(DNQiangzhuangTimes[tableconf.xia_zhu_type]) do
        if qiang_times_must[xia_times] == nil then
            qiang_times_must[xia_times] = 0
        end
        qiang_times_must[xia_times] = 3 * (in_game_num -1) * xia_times * tableconf.score_diamond 
    end
    if qiang_times_must[times] == nil then
        is_can_qiang = false
    else
        if tableconf.table_type == DNTableType.TABLE_TYPE_ZUANSI then
            if money.diamond < qiang_times_must[times] then
                is_can_qiang = false
            end
        elseif tableconf.table_type == DNTableType.TABLE_TYPE_COIN then
            if money.coin < qiang_times_must[times] then
                is_can_qiang = false
            end
        end
    end
    return is_can_qiang
end

function RobotLogic:delay(arg)
    local argtype = type(arg)
    if argtype == "number" then
        skynet.sleep(arg * 100)
    elseif argtype == "table" then
        local tm = get_random_result_by_weightlist(arg,100)
        skynet.sleep(tm * 100)
    elseif argtype == "string" then
        local tm = 0
        local name = "_" .. arg .. "_delay"
        if self[name] then
            tm = get_random_result_by_weightlist(self[name],100)
        else
            tm = base.get_random(1,3)
        end
        skynet.sleep(tm * 100)
    else
        skynet.sleep(base.get_random(1,3) * 100)
    end
end

function RobotLogic:entertableR(msghead, msgbody)
    self.service.table_type = msgbody.game_info.table_conf.table_type
    --调用父类实现
    self:super(msghead, msgbody)
end

function RobotLogic:sitdowntableN(msghead, msgbody)
    ----初始化桌子数据
    if msgbody.rid ~= self.service.rid then 
        return
    end
    self.service:init_tableinfo()
    --调用父类实现
    self:super(msghead, msgbody) 
end

--[Comment]
--准备
--
function RobotLogic:doreadyN(msghead, msgbody)
    --延时
    local gameinfo = self.service.gameinfo
    local round_max = 5

    if msgbody.rid ~= self.service.rid then return end
    if gameinfo.round_num >= 2  and gameinfo.round_num < round_max then
        local random_score = base.get_random(1,100)
        if random_score >= 1 and random_score <= 50 then
            skynet.sleep(2 * 100)
            self.service:leavetable()
        end
    elseif gameinfo.round_num >= round_max then
        skynet.sleep(2 * 100)
        self.service:leavetable()
    else
        self:delay("ready")
    end
    --调用父类实现
    self:super(msghead, msgbody)
end

--[Comment]
--游戏开始
--
function RobotLogic:gamestartN(msghead, msgbody)
    --初始化桌子数据
    self.service:init_tableinfo()
    local gameinfo = self.service.gameinfo
    gameinfo.round_num = gameinfo.round_num + 1

    gameinfo.cur_score = 0

    if gameinfo.start_time == 0 then
        gameinfo.start_time = timetool.get_time()
    end
end

function RobotLogic:reentertableR(msghead, msgbody)
    self.service.table_type = msgbody.game_info.table_conf.table_type
    ---调用父类同名方法
    self:super(msghead, msgbody)
    ----初始化桌子数据
    self.service:init_tableinfo()
end



--[Comment]
--玩家操作
--
function RobotLogic:todoN(msghead, msgbody)
    --调用父类同名方法
    ---self:super(msghead,msgbody)
    local rid = self.service.rid
    local table_info = self.service:get_tableinfo()
    if msgbody.rid ~= rid then return end
    if msgbody.action_type == DNActionType.ACTION_QIANGZHUANG then
        ----判断玩法
        local tableconfinfo = table_info.tableinfo
        if tableconfinfo.play_game_type == DNQZType.QZ_TYPE_FREE then
            ----自由抢庄不拉数据
            self.service:init_tableinfo()
            table_info = self.service:get_tableinfo()
            local requestmsg = {
                action_type = DNActionType.ACTION_BUQIANG,
                times = 1,
            }
            local qiang_times = get_free_qiang_times(100, table_info.self_info, table_info.other_info, table_info.tableinfo.is_open_flush)
            if qiang_times == 1 then
            elseif qiang_times > 1 and qiang_times <= 5 then
                requestmsg.action_type = DNActionType.ACTION_QIANGZHUANG
                requestmsg.times = qiang_times
            end
            if requestmsg.action_type == DNActionType.ACTION_QIANGZHUANG then
                local is_can = get_can_qiang(self.service.money, table_info.tableinfo, requestmsg.times, table_info.tableinfo.in_game_num)
                if is_can == false then
                    requestmsg.action_type = DNActionType.ACTION_BUQIANG
                    requestmsg.times = 1
                end
            end
            --延时后发送操作
            self:delay("doaction")
            self.service:doaction(requestmsg)
        elseif tableconfinfo.play_game_type == DNQZType.QZ_TYPE_KANPAI then
            ----看牌抢庄先拉数据
            self.service:init_tableinfo()
            table_info = self.service:get_tableinfo()
            local cardsinfo = table_info.seatinfo[rid].cards
            local niu_type = cardTool.is_has_niu(cardsinfo)
            local requestmsg = {
                action_type = DNActionType.ACTION_BUQIANG,
                times = 1,
            }
            if niu_type == -1 then
                requestmsg.times = 1
            elseif niu_type >=1 and niu_type <3 then
                requestmsg.action_type = DNActionType.ACTION_QIANGZHUANG
                requestmsg.times = math.random(1,2)
            elseif niu_type >=3 and niu_type < 6 then
                requestmsg.action_type = DNActionType.ACTION_QIANGZHUANG
                requestmsg.times = math.random(1,3)
            elseif niu_type >= 6 and niu_type < 9 then
                requestmsg.action_type = DNActionType.ACTION_QIANGZHUANG
                requestmsg.times = math.random(2,4)
            elseif niu_type >= 9 then
                requestmsg.action_type = DNActionType.ACTION_QIANGZHUANG
                requestmsg.times = math.random(3,4)
            end
            --延时后发送操作
            self:delay("doaction")
            self.service:doaction(requestmsg)
        end
    elseif msgbody.action_type == DNActionType.ACTION_XIAZHU then
        self.service:init_tableinfo()
        table_info = self.service:get_tableinfo()
        local get_xiazhu_times = get_xiazhu_times(table_info.self_info, table_info.other_info, table_info.tableinfo.banker_seat_index, 
            table_info.tableinfo.is_open_flush)
        local requestmsg = {
            action_type = DNActionType.ACTION_XIAZHU,
            times = get_xiazhu_times or 1,
        }
        --延时后发送操作
        self:delay("doaction")
        self.service:doaction(requestmsg)
    elseif msgbody.action_type == DNActionType.ACTION_KAIPAI then
        local requestmsg = {
            action_type = DNActionType.ACTION_KAIPAI,
            times = 1,
        }
        --延时后发送操作
        self:delay("doaction")
        self.service:doaction(requestmsg)
    end
end

function RobotLogic:dealcardsN(msghead, msgbody)
    local rid = self.service.rid
    if msgbody.rid ~= self.service.rid then return end
    local table_info = self.service:get_tableinfo()
end

function RobotLogic:gamebalanceN(msghead, msgbody)
    --保存本局得分
    local resultlist = msgbody.player_infos
    for _,item in pairs(resultlist) do
        if item.rid == self.service.rid then
            self.service.gameinfo.cur_score = item.get_scores or 0
            self.service.gameinfo.total_score = item.all_scores or 0
        end
    end
end

function RobotLogic:noticebankerN(msghead, msgbody)

end

function RobotLogic:jackoptchangeN(msghead, msgbody)

end

function RobotLogic:popmessageN(msghead, msgbody)
end

function RobotLogic:getjackoptN(msghead, msgbody)
end

return RobotLogic